<?php
class Language extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'Language';
    }

    public function rules()
    {
        return array(
                array('l, name', 'required'),
                array('l', 'length', 'max'=>2),
                array('primary', 'length', 'max'=>1),
                array('name', 'length', 'max'=>10),
                array('id, l, name', 'safe', 'on'=>'search'),
        );
    }

    public function relations()
    {
        return array(
                'lMessages' => array(self::HAS_MANY, 'LMessage', 'language'),
                'pages' => array(self::HAS_MANY, 'Page', 'lang_id'),
        );
    }

    public function attributeLabels()
    {
        return array(
                'id'    => 'ID',
                'l'     => Yii::t('language', 'Язык'),
                'name'  => Yii::t('language', 'Расшифровка аббревиатуры'),
        );
    }
        
    public static function dropDownItemsAll(){
        $items= Array();
        $model = self::model()->findAll();
        foreach ($model as $value) {
            $items[$value->id] = $value->name;
        }
        return $items;
    }

    public static function allLang(){
        $dataReader=Yii::app()->db->createCommand()
        ->select('l,name')
        ->from(array('Language'))
        ->queryAll();
        return $dataReader;
    }
    
    public static function getIdDefalutLanguage(){
        return self::getIdForLang(Yii::app()->params['dafaultLanguage']);
    }
    
    public static function countLang(){
        $sql = "SELECT COUNT(*) as count FROM `Language`";
        $dataReader=Yii::app()->db->createCommand($sql)->queryRow();
        return $dataReader['count'];
    }
    
    public static function allExcept($except_id){
        $dataReader=Yii::app()->db->createCommand()
        ->select('*')
        ->from(array('Language'))
        ->where(array('and', '`primary` <> :primary', array('not in','id',$except_id)),array(':primary'=>'1'))
        ->queryAll();
        return $dataReader;
    }
    
    public static function  getLangForId($id){
        $dataReader=Yii::app()->db->createCommand()
        ->select('name')
        ->from(array('Language'))
        ->where('id=:id',array(':id'=>(int)$id))
        ->queryScalar();
        return $dataReader;
    }

    public static function getIdForLang($language){
        $dataReader=Yii::app()->db->createCommand()
        ->select('id')
        ->from(array('Language'))
        ->where('l=:language',array(':language'=>$language))
        ->queryScalar();
        return $dataReader;
    }

    public static function getId(){
        $dataReader=Yii::app()->db->createCommand()
        ->select('id')
        ->from(array('Language'))
        ->where('l=:language',array(':language' => Yii::app()->language))
        ->queryScalar();
        return $dataReader;
    }

    
    public function search()
    {	
        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('l',$this->l,true);
        $criteria->compare('name',$this->name,true);

        return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
        ));
    }
}